package br.com.sgi.autentication;



import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import br.com.sgi.model.Users;
import br.com.sgi.model.UsersIgreja;
import br.com.sgi.service.UsersIgrejaService;

@Component("usuarioLogado")
@Scope("session")
public class UsuarioLogado{
	
	private static Authentication usuarioAuthentication;
	
	private static Long coSistema;
	
	@Autowired
	private UsersIgrejaService usersIgrejaService;
	
	private static String ROLE_USUARIO_LOCAL = "ROLE_USUARIO_LOCAL";
	private static String ROLE_ADMIN_REGIONAL= "ROLE_ADMIN_REGIONAL";
	private static String ROLE_ADMIN_LOCAL= "ROLE_ADMIN_LOCAL";
	private static String ROLE_ADMIN_GERAL= "ROLE_ADMIN_GERAL";
	
	
	private static Set<String> listaCoIgrejas;
	
	
	private boolean usuario;
	private static Set<String> roles = new HashSet<String>();
	
	public UsuarioLogado(){
		init();
	}
	
	private Users getUsuarioSistema ()
	{
		return ( Users )SecurityContextHolder.getContext().getAuthentication().getPrincipal();
	}
	
	private static void init() {
		usuarioAuthentication = SecurityContextHolder.getContext().getAuthentication();
		roles = new HashSet<String>();
		for (GrantedAuthority authority : usuarioAuthentication.getAuthorities()) {
			roles.add(authority.getAuthority());
			System.out.println ( authority.getAuthority() );
		}
		
	}

	public static Long getCoSistema() {
		coSistema = coSistemaUsuarioLogado();
		return coSistema;
	}
	
	private static Long coSistemaUsuarioLogado(){
//		if(getUsuarioLogado().getSistema() == null)
		return getUsuarioLogado().getSistema().getCoSeqSistema();
	}

	private Long coIgrejaFrequenciaUsuarioLogado(){
		return getUsuarioLogado().getSistema().getCoSeqSistema();
	}
	
	/**
	 * Conforme o usuario, obtem os codigos das igrejas que possue permissao
	 * @return
	 */
	public static String getCoIgrejas() {
		init();
		listaCoIgrejas = new HashSet<String>(); 
		if(has(ROLE_USUARIO_LOCAL) || has(ROLE_ADMIN_LOCAL))
			listaCoIgrejas.add(getCoIgrejaLocal());
		if(has(ROLE_ADMIN_REGIONAL))
			listaCoIgrejas.add(getCoIgrejaLocal());
		
		return codigoIgrejas(listaCoIgrejas);
	}

	private void addCoIgrejasAutorizadas(Set<String> listaCoIgrejas){
		List<UsersIgreja> listaIgrejasAutorizadas = usersIgrejaService.buscarUsersIgrejaPorCoUsers(getCoUsuario());
		for(UsersIgreja usersIgreja: listaIgrejasAutorizadas){
			listaCoIgrejas.add(usersIgreja.getIgreja().toString());
		}
		
	}
	
	private String getCoUsuario(){
		return getUsuarioLogado().getUsername();
	}
	
	/**
	 * cria a lista com os codigos das igrejas
	 * @param listaCodigos
	 * @return
	 */
	private static String codigoIgrejas(Set<String> listaCodigos){
		StringBuffer codigos = new StringBuffer();
		for(String codigo: listaCodigos){
			if(codigos.length() == 0)
				codigos.append(codigo);
			else{
				codigos.append(",");
				codigos.append(codigo);
			}
		}
		
		return codigos.toString(); 
	}
	
	
	public static boolean isAdminGeral(){
		init();
		return has(ROLE_ADMIN_GERAL);
	}


	
	public Users getUsuarioAuthentication() {
		return (Users)usuarioAuthentication.getPrincipal();
	}
	
	public static Users getUsuarioLogado(){
		return ( Users )SecurityContextHolder.getContext().getAuthentication().getPrincipal();
	}
	
	private static String getCoIgrejaLocal(){
		return getUsuarioLogado().getIgreja().getCoSeqIgreja().toString();
	}
	
	public static String getUsernameUsuarioLogado(){
		return getUsuarioLogado().getUsername();
	}
	
	public boolean isUsuario() {
		return usuario;
	}

	public void setUsuario(boolean usuario) {
		this.usuario = usuario;
	}

	public static Set<String> getRoles( )
	{
		return roles;
	}
	
	public static boolean has ( String role )
	{
		return getRoles( ).contains( role );
	}
}